//index.js
//获取应用实例
const app = getApp()
const api_url = 'https://douban.uieee.com/v2/movie/in_theaters';
Page({
  data: {
    movies: [],
    title: "加载中..",
    pageSize: 20,
    pageIndex: 1,
  },
  //事件处理函数
  bindViewTap: function () {

  },
  //下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();
    wx.request({
      url: api_url,
      data: {

      },
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Content-Type': "json"
      },
      success: (res) => {
        wx.hideToast();
        let data = res.data;
        this.setData({
          title: data.title,
          movies: data.subjects
        });
      }
    })
    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();
    // 停止下拉动作  
    wx.stopPullDownRefresh();
  },
  //上拉加载更多
  onReachBottom: function () {
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });
    wx.request({
      url: api_url,
      data: {
        start: this.data.pageIndex * this.data.pageSize
      },
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Content-Type': "json"
      },
      success: (res) => {
        this.setData({
          movies: this.data.movies.concat(res.data.subjects),
          pageIndex: this.data.pageIndex + 1
        });
        wx.hideLoading();
      }
    })
  },
  onLoad: function () {
    wx.showToast({
      title: '加载中...',
      icon: "loading",
      duration: 2000
    });
    wx.request({
      url: api_url,
      data: {

      },
      method: "GET",
      header: {
        'content-type': 'application/json',
        'Content-Type': "json"
      },
      success: (res) => {
        wx.hideToast();
        let data = res.data;
        this.setData({
          title: data.title,
          movies: data.subjects
        });
      }
    })
  },
  getUserInfo: function (e) {
  }
})
